<template>
	<view class="page">
		<view class="title">
			
			<view class="title_item" v-for="(item,index) in optionList" :key="index" @click="selectItem(index)">
				<view :class="item.isSelect?'selectItem':'title_item'">
					<text>{{item.name}}</text>
				</view>
			</view>
		</view>
		<view v-if="homeworkList.length > 0" class="homeworkItem" @click="jumpToHomework(item)" v-for="(item, index) in homeworkList" :key="index">
			<view class="homeworkItem_title">
				<text>{{item.name}}</text>
			</view>
			<view class="homeworkItem_time">
				<text>{{item.time}}</text>
			</view>
		</view>
		<view v-if="homeworkList.length < 1">
			<view class="null">
				<image src="../../static/classnull.png" mode="widthFix"></image>
				<view style="justify-content: center; font-size: medium; width: 428rpx; margin-left: 280rpx; margin-top: 70rpx; color: #707070;">暂时没有作业</view>
			</view>
		</view>
	</view>
</template>

<script>
	import cloudbase from '@cloudbase/js-sdk';
	
	const db = cloudbase.database();
	const dbCmd = db.command;
	export default {
		data() {
			return {
				// 教师或者是学生
				type: '',
				optionList:[],
				studentList:[
					{
						name:'未完成',
						isSelect:true,
						type:'学生'
					},{
						name:'已完成',
						isSelect:false,
						type:'学生'
					}
				],
				teacherOption:[
					{
						name:'待发起',
						isSelect:true,
						type:'教师'
					},{
						name:'未批改',
						isSelect:false,
						type:'教师'
					},{
						name:'已批改',
						isSelect:false,
						type:'教师'
					}
				],
				// 学生作业列表
				homeworkList:[],
				// 未完成作业
				notFinishList:[],
				// 已完成作业
				finishList:[],
				// 待发起作业
				toInitiateList:[],
				// 未批改作业
				notCorrectList:[],
				// 已批改作业
				correctList:[],
				homeworkAll:[]
			}
		},
		onLoad(e) {
			if(e.type) this.type = e.type;
			if(e.homeworkAll) this.homeworkAll = JSON.parse(e.homeworkAll);
			if(this.type == "学生"){
				this.initStudent();
			}else{
				this.toInitiateList = [];
				this.initTeacher();
			}
		},
		methods: {
			initStudent: function(){
				this.optionList = this.studentList;
				this.homeworkList = this.homeworkAll;
			},
			initTeacher: function(){
				this.optionList = this.teacherOption;
				this.homeworkList = this.toInitiateList;
				// 获取班级
				db.collection('class').where({
					institutionId: getApp().globalData.userInfo.institutionId,
					teacher: getApp().globalData.userInfo.openId
				}).get().then(res=>{
					// console.log(res.data);
					let result = res.data;
					result.forEach(items => {
						let classId = items._id;
						let homeworkList = items.homeworkList;
						// 遍历课程
						items.courses.forEach(item => {
							// 获取课程的名称
							db.collection('course').where({
								_id: item._id
							}).get().then(res => {
								// console.log(res);
								let name = res.data[0].name;
								let className = items.name;
								let time = null;
								let day = new Date().getDay() - 1;
								if(day == -1) day = 6;
								let schedule = item.time[day];
								if(schedule != null){
									schedule.forEach(item => {
										switch (item) {
											case '第一节':
												time = '08:00~10:00';
												break;
											case '第二节':
												time = '10:00~12:00';
												break;
											case '第三节':
												time = '14:00~16:00';
												break;
											case '第四节':
												time = '16:00~18:00';
												break;
											case '第五节':
												time = '19:00~21:00';
												break;
										}
										// console.log(time)
										let date = new Date();
										if (time != null) {
											let today = date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate() + ' ' + time;
											// 判断是否已经布置过作业了
											let flag = false;
											if(homeworkList){
												homeworkList.forEach(item=>{
													console.log(item);
													if(item.time == today) {
														// 发布过作业了
														flag = true;
													}
												})
											}
											if(!flag){
												this.toInitiateList.push({
													name: className + '（' + name + '）',
													time: today,
													course: name,
													_id: classId
												})
											}
										}
									})
									// 获取待批改和已批改的作业
									homeworkList.forEach(item=>{
										this.alreadyHomeworkList(item, className, name, classId);
									})
								}
							})
						})
					})
				})
			},
			alreadyHomeworkList: function(item, className, name, classId) {
				let notCorrectFlag = false;
				let correctFlag = false;
				// 判断是否所有学生都批改了，只要有的学生分数为空，说明未批改
				if(item.homework.length > 0){
					item.homework.forEach(stu=>{
						if(stu !=  null && stu.score == '') {
							notCorrectFlag = true;
						}else if(stu !=  null && stu.score != '') {
							correctFlag = true;
						}
					})
				}
				// if(item.homework.length == 0) stuFlag = true;
				if(notCorrectFlag){
					this.notCorrectList.push({
						name: className + '（' + name + '）',
						time: item.time,
						endTime: item.endTime,
						feedback: item.feedback,
						homework: item.homework,
						students: item.students,
						subject: item.subject,
						course: name,
						_id: classId,
					})
				}
				if(correctFlag){
					this.correctList.push({
						name: className + '（' + name + '）',
						time: item.time,
						endTime: item.endTime,
						feedback: item.feedback,
						homework: item.homework,
						students: item.students,
						subject: item.subject,
						course: name,
						_id: classId,
					})
				}
			},
			selectItem: function(index){
				for(let i in this.optionList){
					this.optionList[i].isSelect = false;
					if(i == index){
						this.optionList[i].isSelect = true;
						switch(this.optionList[i].name){
							case '未完成':
								this.homeworkList = this.homeworkAll;
								break;
							case '已完成':
								this.showFinishList();
								break;
							case '待发起':
								this.homeworkList = this.toInitiateList;
								break;
							case '未批改':
								this.homeworkList = this.notCorrectList;
								break;
							case '已批改':
								this.homeworkList = this.correctList;
								break;
						}
					}
				}
			},
			showFinishList: function(){
				let that = this;
				// 获取该学生加入的班级
				db.collection('class').where({
					institutionId: getApp().globalData.userInfo.institutionId,
					students: dbCmd.all([getApp().globalData.userInfo.openId])
				}).get().then(res => {
					let result = res.data;
					that.finishList = [];
					result.forEach(items => {
						let homeworkList = items.homeworkList;
						let openId = getApp().globalData.userInfo.openId;
						that.homeworkList = that.finishList;
						if(homeworkList){
							// console.log(homeworkList);
							homeworkList.forEach(item=>{
								if(item.students.indexOf(openId) != -1){
									console.log(item);
									item.homework.forEach(work=>{
										if(work){
											if(work.openid == openId){
												that.finishList.push({
													endTime: item.endTime,
													feedback: item.feedback,
													name: item.name,
													subject: item.subject,
													time: item.time,
													_id:  items._id,
													homework: work
												});
												return;
											}
										}
									})
								}
							})
						}
					})
				})
			},
			jumpToHomework: function(item){
				let option;
				this.optionList.forEach(item=>{
					if(item.isSelect){
						option = item.name;
					}
				})
				console.log(option)
				if(option == '未批改'){
					// 未批改的处理方法
					uni.navigateTo({
						url: '../classMembers/classMembers?item=' + JSON.stringify(item) + '&option=未批改'
					})
				} 
				else if(option == '已批改'){
					// 已批改的处理方法
					uni.navigateTo({
						url: '../classMembers/classMembers?item=' + JSON.stringify(item) + '&option=' + '已批改' 
					})
				}else{
					// 正常的跳转
					uni.navigateTo({
						url:"../homeworkMore/homeworkMore?homework=" + JSON.stringify(item) + '&option=' + option
					})
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: #F3F5F7; 
	}
	.page {
		width: 750rpx;
		/* height: 1020rpx; */
		/* padding-bottom: 60px; */
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
	}
	.title{
		width: 702rpx;
		height: 50rpx;
		margin-top: 24rpx;
		margin-left: auto;
		margin-right: auto;
		font-size: 36rpx;
		line-height: 50rpx;
		/* background-color: #0000FF; */
		color: #121111;
	}
	.title_item{
		width: 108rpx;
		height: 50rpx;
		text-align: center;
		display: inline-block;
		/* font-weight: 600; */
		margin-right: 35rpx;
	}
	.selectItem{
		width: 108rpx;
		height: 50rpx;
		text-align: center;
		font-weight: bold;
		display: inline-block;
		margin-right: 35rpx;
		padding-bottom: 5rpx;
		border-bottom: solid 2px #FF9933;
	}
	.homeworkItem{
		width: 702rpx;
		height: 161rpx;
		margin-top: 24rpx;
		margin-right: auto;
		margin-left: auto;
		background-color: #FFFFFF;
		border-radius: 24rpx;
	}
	.homeworkItem_title{
		width: 702rpx;
		height: 42rpx;
		font-size: 32rpx;
		line-height: 42rpx;
		margin-top: 36rpx;
		margin-bottom: 15rpx;
		text-align: center;
		color: #222222;
	}
	.homeworkItem_time{
		width: 702rpx;
		height: 42rpx;
		font-size: 30rpx;
		line-height: 42rpx;
		text-align: center;
		color: #707070;
		user-select: none;
	}
	
	.null {
		width: 428rpx;
		/* height: 400rpx; */
		display: flex;
		flex-direction: column;
		color: #707070;
		margin-top: 50rpx;
	}
	.null image {
		width: 428rpx;
		/* height: 240rpx; */
		margin-left: 160rpx;
		margin-top: 100rpx;
		justify-content: center;
	}
</style>
